package irri.soils.ssnm;

import irri.soils.ssnm.R;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityMain5 extends SSNM implements View.OnClickListener, TextWatcher{
    /** Called when the activity is first created. */
	Cursor c;
	private Bundle b;
	private Intent intent;
	private TextView exten_name;
	private TextView exten_num;
	private TextView exten_email;
	private Button button1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main5);
        setCustomTitle();
        
        exten_name = (TextView) findViewById(R.id.exten_name);
        exten_num = (TextView) findViewById(R.id.exten_num);
        exten_email = (TextView) findViewById(R.id.exten_email);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        
        SQLiteDatabase myDB= null;
       
        try {
         myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
         myDB.execSQL("CREATE TABLE IF NOT EXISTS owner2 (name varchar(50) " +
         		"NOT NULL, num varchar(12) NOT NULL, email varchar(50) " +
         		"NOT NULL);");
         
         Cursor c = myDB.rawQuery("SELECT count(*) FROM owner2", null);
         if (c != null) {
        	 
             c.moveToFirst();
             if(c.getInt(0) > 0){
                 intent = new Intent(ActivityMain5.this,
          				SSNM.prevActivity==null||
          				this.getIntent().getExtras()==null
          				? ActivityMain.class : SSNM.prevActivity.getClass());
            	 Cursor c2 = myDB.rawQuery("SELECT * FROM owner2" , null);
            	 
            	 if(this.getIntent().getExtras()!=null)
            		 b = this.getIntent().getExtras();
            	 else b = new Bundle();
            	 
         		 if(c2 != null){
            		 c2.moveToFirst();
             		 b.putString("exten_name", c2.getString(0));
             		 b.putString("exten_num", c2.getString(1));
             		 b.putString("exten_email", c2.getString(2));
             		 c2.close();
            	 }
         		intent.putExtras(b);
             }else{
        		 b = this.getIntent().getExtras();
        		 myDB.execSQL("INSERT INTO owner2 (name, num, email) " +
        		 		"VALUES ('"+
        				 b.getString("exten_name")+"', '"+
        				 b.getString("exten_num")+"', '"+
        				 b.getString("exten_email")+"');");
        		 exten_name.setText(b.getString("exten_name"));
        		 exten_num.setText(b.getString("exten_num"));
        		 exten_email.setText(b.getString("exten_email"));
        		 
             }
             c.close();
         }
        }
        catch(Exception e) {
         Log.e("Error", "Error", e);
        } finally {
         if (myDB != null)
          myDB.close();
        }
        if(intent!=null){ 
        	intent.putExtras(b);
        	startActivity(intent);
        	prevActivity = null;
        	finish();
        }
        exten_name.addTextChangedListener(this);
        exten_num.addTextChangedListener(this);
        exten_email.addTextChangedListener(this);
        appearNext();
        
    }
    
    public void appearNext(){
    	button1.setEnabled(!exten_name.getText().toString().equals("")&&
    			!exten_email.getText().toString().equals("")&&
    			!exten_num.getText().toString().equals(""));
    }

	@Override
	public void onClick(View v) {
		if(exten_num.getText().toString().length()==11){
		SQLiteDatabase myDB= null;
		try {
		 myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
		 myDB.execSQL("CREATE TABLE IF NOT EXISTS owner2 (name varchar(50) " +
	         		"NOT NULL, num varchar(12) NOT NULL, email varchar(50) " +
	         		"NOT NULL);");
		 myDB.execSQL("DELETE FROM owner2;");
		 System.out.println("tester");
	     myDB.execSQL("INSERT INTO owner2 (name, num, email) " +
 		 		"VALUES ('"+
				 exten_name.getText()+"', '"+
				 exten_num.getText()+"', '"+
				 exten_email.getText()+"');");
	     System.out.println("INSERT INTO owner2 (name, num, email) " +
	 		 		"VALUES ('"+
					 exten_name.getText()+"', '"+
					 exten_num.getText()+"', '"+
					 exten_email.getText()+"');");
	     intent = new Intent(ActivityMain5.this, SSNM.prevActivity==null ? 
	    		 ActivityMain.class : SSNM.prevActivity.getClass());
	     b = this.getIntent().getExtras();
    	 b.putString("exten_name", ""+exten_name.getText());
    	 b.putString("exten_num", ""+exten_num.getText());
    	 b.putString("exten_email", ""+exten_email.getText());
 		 intent.putExtras(b);
		}catch(Exception e) {
			exten_name.setText(e.toString());
        } finally {
         if (myDB != null) myDB.close();
        }
        if(intent!=null){
        	intent.putExtras(b);
        	startActivity(intent);
        }
        prevActivity = null;
        finish();
		}else{
			displayToast(get("invalid"));
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	@Override
	public void onBackPressed() {
		if(SSNM.prevActivity!=null){
			intent = new Intent(ActivityMain5.this, SSNM.prevActivity.getClass());
		    b = this.getIntent().getExtras();
		    intent.putExtras(b);
		    startActivity(intent);
		    prevActivity = null;
		}
	    finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		appearNext();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}
}